package com.liujit.standard.basis.core.handler;

import com.liujit.standard.basis.core.enums.ResultCode;
import com.liujit.standard.basis.core.exception.BusinessException;
import com.liujit.standard.basis.core.result.Result;
import com.liujit.standard.basis.core.util.CoreUtil;
import com.liujit.standard.basis.core.valid.ParameterInvalidItem;
import com.liujit.standard.basis.core.valid.ParameterInvalidItemHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.util.List;

/**
 * @description: 全局异常处理类
 * @author: liujun
 * @create: 2021/1/4 10:20 上午
 **/
@Slf4j
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 违反约束异常
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public Result handleConstraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
        log.info("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);
        List<ParameterInvalidItem> parameterInvalidItemList = ParameterInvalidItemHelper.convertCVSetToParameterInvalidItemList(e.getConstraintViolations());
        String msg = CoreUtil.getMsg(parameterInvalidItemList);
        return Result.failure(msg);
    }

    /**
     * 处理参数绑定时异常
     */
    @ExceptionHandler(BindException.class)
    public Result handleBindException(BindException e, HttpServletRequest request) {
        log.info("handleBindException start, uri:{}, caused by: ", request.getRequestURI(), e);
        List<ParameterInvalidItem> parameterInvalidItemList = ParameterInvalidItemHelper.convertBindingResultToMapParameterInvalidItemList(e.getBindingResult());
        String msg = CoreUtil.getMsg(parameterInvalidItemList);
        return Result.failure(msg);
    }

    /**
     * 处理使用@Validated注解时，参数验证错误异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
        log.info("handleMethodArgumentNotValidException start, uri:{}, caused by: ", request.getRequestURI(), e);
        List<ParameterInvalidItem> parameterInvalidItemList = ParameterInvalidItemHelper.convertBindingResultToMapParameterInvalidItemList(e.getBindingResult());
        String msg = CoreUtil.getMsg(parameterInvalidItemList);
        return Result.failure(msg);
    }

    /**
     * 处理通用自定义业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public Result handleBusinessException(BusinessException e, HttpServletRequest request) {
        log.info("handleBusinessException start, uri:{}, exception:{}, caused by: {}", request.getRequestURI(), e.getClass(), e.getMessage());
        return Result.failure(e.getCode(), e.getMsg());
    }

    /**
     * 处理未预测到的其他错误（反500错误码）
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Throwable.class)
    public Result handleThrowable(Throwable e, HttpServletRequest request) {
        log.error("handleThrowable start, uri:{}, caused by: ", request.getRequestURI(), e);
        return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
    }

}
